import axios from 'axios';
import { Loading, Message } from 'element-ui';

export default function (options) {
    return new Promise((resolve, rejected) => {
        if (options.loading !== false) {
            var loading = Loading.service({
                lock: true,
                background: 'rgba(255, 255, 255, 0.2)'
            });
        };
        return axios({
            method: options.type,
            url: options.url,
            data: options.data ? options.data : undefined,
            headers: options.headers ? options.headers : undefined,
            params: options.params ? options.params : undefined,
        }).then((res) => {
            if (options.loading !== false) {
                loading.close();
            };
            resolve(res.data);
        }).catch((error) => {
            if (options.loading !== false) {
                loading.close();
            }
            if (error.response) {
                Message({
                    message: error.response.data.msg,
                    type: 'error'
                }, true);
            } else {
                rejected(error);
            };
        });
    });
};



